/**
 *
 */
package com.naiterui.ehp.bs.recommend.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.recommend.enums.DrugCycleUnitEnum;

/**
 * @since 2.14
 */
public class RecomBizUtil {
    /**
     * 去掉数字后多余的0
     * Remoter.subZeroAndDot()
     * @Author lichaopi
     * @Date 2017年12月21日
     * @since 1.0.0
     * @param s
     * @return
     */

    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");// 去掉多余的0
            s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 计算用药周期的天数,可以有小数
     * RecomBizUtil.getDrugCycle()
     *
     * @param durgCycle
     * @param drugCycleUnit
     *
     * @return
     *
     * @Date 2018年6月28日
     * @since 2.19
     */
    public static Double getDrugCycle(Double durgCycle, String drugCycleUnit) {
        if (durgCycle == null) {
            return 0D;
        }
        DrugCycleUnitEnum unitEnum = DrugCycleUnitEnum.valuesOf(drugCycleUnit);
        if (unitEnum == DrugCycleUnitEnum.WEEK) {
            durgCycle = durgCycle * 7;
        } else if (unitEnum == DrugCycleUnitEnum.MONTH) {
            durgCycle = durgCycle * 30;
        } else if (unitEnum == DrugCycleUnitEnum.HOUR) {
            durgCycle = durgCycle / 24;
        }
        return durgCycle;
    }

    public static Double getDrugFrequency(Double dosageCycle, String dosageCycleUnit, int dosageCount) {
        if (StringUtils.isEmpty(dosageCycleUnit)) {
            return 0D;
        }
        DrugCycleUnitEnum unitEnum = DrugCycleUnitEnum.valuesOf(dosageCycleUnit);
        if (unitEnum == DrugCycleUnitEnum.WEEK) {
            dosageCycle = dosageCount / dosageCycle / 7;
        } else if (unitEnum == DrugCycleUnitEnum.MONTH) {
            dosageCycle = dosageCount / dosageCycle / 30;
        } else if (unitEnum == DrugCycleUnitEnum.HOUR) {
            dosageCycle = dosageCount / dosageCycle * 24;
        }
        return dosageCycle;
    }

    /**
     * 计算用药周期的天数比较
     *
     * @param durgCycle1
     * @param drugCycleUnit1
     * @param durgCycle2
     * @param drugCycleUnit2
     *
     * @return
     */
    public static int compareDrugFrequency(Double cycle, String unit, int count,
                                           Double oCycle, String oUnit, int oCount) {
        return ObjectUtils.compare(getDrugFrequency(cycle, unit, count), getDrugFrequency(oCycle, oUnit, oCount));
    }

    /**
     * 计算用药周期的天数比较
     *
     * @param durgCycle1
     * @param drugCycleUnit1
     * @param durgCycle2
     * @param drugCycleUnit2
     *
     * @return
     */
    public static int compareDrugCycle(Double cycle, String unit, Double oCycle, String oUnit) {
        return ObjectUtils.compare(getDrugCycle(cycle, unit), getDrugCycle(oCycle, oUnit));
    }

    /**
     * 是否科技算用药提醒
     *
     * @param cycleUsage
     *
     * @return
     */
    public static boolean canDrugRemind(String cycleUsage) {
        return ConfigUtil.getString(Constants.RECOM_DRUG_REMIND).contains(cycleUsage);
    }

    /**
     * 周期提醒计算
     *
     * @param cycleUsage
     *
     * @return
     */

    public static List<Integer> getDrugRemind(String cycleUsage) {
        List<String> cycleList = Arrays.asList(ConfigUtil.getString(Constants.RECOM_DRUG_REMIND).split(","));
        List<String> cycleRemindList = Arrays.asList(ConfigUtil.getString(Constants.RECOM_DRUG_REMIND_ITEM).split(","));
        int index = cycleList.indexOf(cycleUsage);
        if (index < 0 || cycleRemindList.size() - 1 < index) {
            return null;
        }
        String remindItem = cycleRemindList.get(index);
        List<Integer> resultList;
        if (remindItem.startsWith("#")) {
            int cycleStep = Integer.parseInt(remindItem.substring(1));
            int remind = Constants.RECOM_DRUG_REMIND_START;
            int end = Constants.RECOM_DRUG_REMIND_END;
            resultList = new ArrayList<>();
            do {
                resultList.add(remind);
            } while ((remind += cycleStep) <= end);
        } else {
            resultList = Arrays.stream(remindItem.split("&")).map(Integer::parseInt).collect(Collectors.toList());
        }
        return resultList;
    }

    /**
     * 失效时间是否在当日
     *
     * @param dueAt
     *
     * @return
     */
    public static boolean dueToday(Date dueAt) {
        Date gtDate = DateUtil.convertDate(new Date());
        Date ltDate = DateUtil.convertDate(DateUtils.addDays(new Date(), 1));
        return DateUtil.rangeCompare(dueAt, gtDate, ltDate);
    }

}
